﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Com.Alipay;
public partial class Manageweb_alipay_Pay : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.txtAccount.Attributes.Add("readyonly", "true");
            this.txtMemName.Attributes.Add("readyonly", "true");
            this.TxtSubject.Attributes.Add("readyonly", "true");

            BindAccountInfo();
        }
    }

    protected void BindAccountInfo()
    {
        SQLExpressDAL.Users.UsersDataTable tab = Session["UserInfo"] as SQLExpressDAL.Users.UsersDataTable;
        SQLExpressDAL.Users.UsersRow dr = tab.Rows[0] as SQLExpressDAL.Users.UsersRow;

        this.txtAccount.Text = dr.UserID;
        this.txtMemName.Text = dr.UserName;
    }

    protected void BtnAlipay_Click(object sender, EventArgs e)
    {
        Int32 recordID = InsertRechargeRecord();

        if (recordID== -1)
        {
           return;
        }


        RedirectToAlipay(recordID.ToString());
    }

    /// <summary>
    /// 插入充值记录 返回记录的ID
    /// </summary>
    /// <returns></returns>
    protected Int32 InsertRechargeRecord()
    {
        Int32 recordID = -1;

        try
        {
            SQLExpressDAL.DealRecord obj = new SQLExpressDAL.DealRecord();
            recordID = obj.InsertRechargeRecord(Session["UserID"].ToString(), "chongzhi", Convert.ToDecimal(this.TxtTotal_fee.Text), 0);
        }
        catch
        { 
           
        }
        
        if(recordID==-1)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "msg", "alert('操作失败!')", true);
        }

        return recordID;
    }

    protected void RedirectToAlipay(string orderID)
    {
        string out_trade_no = orderID;       
        string subject = TxtSubject.Text.Trim();        
        string body = string.Format("会员:{0} {1} AB1账户充值",this.txtAccount.Text,this.txtMemName.Text); //TxtBody.Text.Trim();         
        string total_fee = TxtTotal_fee.Text.Trim();

         //扩展功能参数——默认支付方式//

        //默认支付方式，代码见“即时到帐接口”技术文档
        string paymethod = "";
        //默认网银代号，代号列表见“即时到帐接口”技术文档“附录”→“银行列表”
        string defaultbank = "";

        //扩展功能参数——防钓鱼//

        //防钓鱼时间戳
        string anti_phishing_key = "";
        //获取客户端的IP地址，建议：编写获取客户端IP地址的程序
        string exter_invoke_ip = "";
        //注意：
        //请慎重选择是否开启防钓鱼功能
        //exter_invoke_ip、anti_phishing_key一旦被设置过，那么它们就会成为必填参数
        //建议使用POST方式请求数据
        //示例：
        //exter_invoke_ip = "";
        //Service aliQuery_timestamp = new Service();
        //anti_phishing_key = aliQuery_timestamp.Query_timestamp();               //获取防钓鱼时间戳函数

        //扩展功能参数——其他//

        //商品展示地址，要用http:// 格式的完整路径，不允许加?id=123这类自定义参数
        string show_url = "http://www.anbang100.com/myorder.aspx";
        //自定义参数，可存放任何内容（除=、&等特殊字符外），不会显示在页面上
        string extra_common_param = "";
        //默认买家支付宝账号
        string buyer_email = "";

        //扩展功能参数——分润(若要使用，请按照注释要求的格式赋值)//

        //提成类型，该值为固定值：10，不需要修改
        string royalty_type = "";
        //提成信息集
        string royalty_parameters = "";
        //把请求参数打包成数组
        SortedDictionary<string, string> sParaTemp = new SortedDictionary<string, string>();
        sParaTemp.Add("payment_type", "1");
        sParaTemp.Add("show_url", show_url);
        sParaTemp.Add("out_trade_no", out_trade_no);
        sParaTemp.Add("subject", subject);
        sParaTemp.Add("body", body);
        sParaTemp.Add("total_fee", total_fee);
        sParaTemp.Add("paymethod", paymethod);
        sParaTemp.Add("defaultbank", defaultbank);
        sParaTemp.Add("anti_phishing_key", anti_phishing_key);
        sParaTemp.Add("exter_invoke_ip", exter_invoke_ip);
        sParaTemp.Add("extra_common_param", extra_common_param);
        sParaTemp.Add("buyer_email", buyer_email);
        sParaTemp.Add("royalty_type", royalty_type);
        sParaTemp.Add("royalty_parameters", royalty_parameters);

        //构造即时到帐接口表单提交HTML数据，无需修改
        Service ali = new Service();
        string sHtmlText = ali.Create_direct_pay_by_user(sParaTemp);
        Response.Write(sHtmlText);
    }
}
